import 'package:permission_handler/permission_handler.dart';

class PermissonMan {
  static Future<bool> location() async {
    final l = await request(Permission.location);
    final always = await request(Permission.locationAlways);
    final use = await request(Permission.locationWhenInUse);
    return l && always && use;
  }

  static Future<bool> audio() async {
    return request(Permission.audio);
  }

  static Future<bool> camera() async {
    return request(Permission.camera);
  }

  static Future<bool> photos() async {
    return request(Permission.photos);
  }

  static Future<bool> microphone() async {
    return request(Permission.microphone);
  }

  static Future<bool> appTrackingTransparency() async {
    return request(Permission.appTrackingTransparency);
  }

  static Future<bool> manageExternalStorage() async {
    return request(Permission.manageExternalStorage);
  }

  static Future<bool> storage() async {
    return request(Permission.storage);
  }

  static Future<bool> request(Permission p) async {
    // 检查存储权限状态
    var status = await p.status;

    // 如果权限未授予，请求权限
    if (status.isDenied) {
      // 用户以前拒绝了权限请求，可能会要求再次解释为什么需要该权限
      status = await p.request();
    }

    // 如果权限被永久拒绝，则需要引导用户去设置中手动打开权限
    if (status.isPermanentlyDenied) {
      // 打开应用的设置页面
      await openAppSettings();
      return false;
    }

    // 如果权限被暂时拒绝，可以再次请求权限
    if (status.isDenied) {
      status = await p.request();
    }

    // 返回权限授予结果
    return status.isGranted;
  }
}
